//
//  YJPushSetViewController.m
//  Sports-Go
//
//  Created by rimi on 16/9/21.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "YJPushSetViewController.h"
#import "NotificationHelper.h"
#import "MessagePushCell.h"
#import "FTT_PickerView.h"
@interface YJPushSetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)NSMutableArray *hourArray;
@property(nonatomic,strong)NSMutableArray *minuteArray;
@end

@implementation YJPushSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"推送设置";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //去掉导航栏返回字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.barTintColor = getColor(58, 206, 248, 1);
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"switch"]){
        NSArray *arr = @[@{@"m1":@"消息推送",@"m2":@"自动提醒健身锻炼"},@{@"m1":@"训练时间",@"m2":@"设置每日提醒时间"},];
        self.dataSource = [arr mutableCopy];
    }else{
    
        NSArray *arr = @[@{@"m1":@"消息推送",@"m2":@"自动提醒健身锻炼"}];
        
        self.dataSource = [arr mutableCopy];
    }
    
   
 
    [self.view addSubview:self.tableView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change_PushY) name:@"change" object:nil];
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change_PushN) name:@"change1" object:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessagePushCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (indexPath.section != 0 )
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.index = indexPath.section;
    cell.dic = self.dataSource[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section != 0) {
        
        NSMutableDictionary *dic = [NSMutableDictionary new];
        [dic setValue:self.minuteArray forKey:@"array"];
        [dic setValue:self.hourArray forKey:@"sex"];
        FTT_PickerView *share = [[FTT_PickerView alloc]initWithFrame:self.view.bounds PickerViewType:FTT_PickerViewShowTypeAlert DataSoucre:dic];
        share.num = 2;
        __weak typeof(self) weakSelf = self;
        [share showCityView:^(NSString *proviceStr, NSString *cityStr, NSString *distr) {
            
            [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%@:%@%@",proviceStr,cityStr,distr] forKey:@"noticeTime"];
            NSString *timeString = [NSString stringWithFormat:@"%@:%@:02",proviceStr,cityStr];
        [[NotificationHelper shareInstance]removeLocalAllNotification];
            
        [[NotificationHelper shareInstance] addNotifiction:timeString keyA:@"key" messageA:@"可以领取体力了！" ];
           
            [weakSelf.tableView reloadData];
           
        }];
    }

}
- (void)change_PushY{
    [self.dataSource removeAllObjects];
    NSArray *arr = @[@{@"m1":@"消息推送",@"m2":@"自动提醒健身锻炼"},@{@"m1":@"训练时间",@"m2":@"设置每日提醒时间"},];
    self.dataSource = [arr mutableCopy];
    [self.tableView reloadData];
}
- (void)change_PushN{
    [self.dataSource removeAllObjects];
    NSArray *arr = @[@{@"m1":@"消息推送",@"m2":@"自动提醒健身锻炼"}];
    
    self.dataSource = [arr mutableCopy];
    [self.tableView reloadData];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screen_size.width, screen_size.height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.rowHeight = 60;
        [_tableView registerClass:[MessagePushCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        }
    
    return _dataSource;
}
- (NSMutableArray *)hourArray{
    if (!_hourArray) {
        _hourArray = [NSMutableArray array];
        for (int i = 0; i < 60; i++) {
   [_hourArray addObject:[NSString stringWithFormat:@"%02d",i]];
        }
    }

    return _hourArray;
}
- (NSMutableArray *)minuteArray{
    if (!_minuteArray) {
        _minuteArray = [NSMutableArray array];
        for (int i = 0; i < 24 ; i++) {
    [_minuteArray addObject:[NSString stringWithFormat:@"%02d",i]];
  
        }
    }
    return _minuteArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES];
     self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
